package com.hzit.proxy.sendmsg;

import com.alibaba.fastjson.JSONObject;
import com.hzit.net.WXNetUtil;
import com.hzit.proxy.base.AccessToken;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发送客服消息
 */
public class SendMessageProxy {

    private String myurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + AccessToken.getToken();

    /**
     * 文本消息
     *
     * @return
     */
    public String sendKfTextMsg(String openid, String content) {

        String msgtype = MsgType.TEXT.getMsgtype();

        Map<String, Object> param = new HashMap<>();
        param.put("touser", openid);
        param.put("msgtype", msgtype);

        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("content", content);
        param.put("text", contentMap);

        String initParam = JSONObject.toJSONString(param);

        String result = WXNetUtil.post(myurl, initParam);

        // { "errcode":0, "errmsg":"ok"}
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getString("errcode").equals("0")) {
            return "OK";
        }

        return "FAILED";
    }

    /**
     * 发送链接图文消息
     *
     * @return
     */
    public String sendKfNewsMsg(String openid, String title, String desc, String url, String picurl) {

        String msgtype = MsgType.NEWS.getMsgtype();
        Map<String, Object> map = new HashMap<>();

        map.put("touser", openid);
        map.put("msgtype", msgtype);

        Map<String, List<Map<String, String>>> newsMap = new HashMap<>();

        List<Map<String, String>> articles = new ArrayList<>();
        Map<String, String> articlesMap = new HashMap<>();
        articlesMap.put("title", title);
        articlesMap.put("description", desc);
        articlesMap.put("url", url);
        articlesMap.put("picurl", picurl);

        articles.add(articlesMap);

        newsMap.put("articles", articles);

        map.put("news", newsMap);

        String initParam = JSONObject.toJSONString(map);

        String result = WXNetUtil.post(myurl, initParam);

        System.out.println("--->result:"+result);
        // { "errcode":0, "errmsg":"ok"}
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getString("errcode").equals("0")) {
            return "OK";
        }

        return "FAILED";
    }

    public static void main(String[] args) {

        //String param = "{\"news\":{\"articles\":[{\"picurl\":\"https://img.meituan.net/msmerchant/d3aa8cbedccf51f30c81b71417f12254711607.jpg@380w_214h_1e_1c\",\"description\":\"宝安区新沙路万科翡逸郡园四期116铺\",\"title\":\"炭家牛韩式烤肉\",\"url\":\"https://www.baidu.com/\"}]},\"touser\":\"oJ1R0w4jz1QaGUMn8SA5n9ud-yIA\",\"msgtype\":\"news\"}";

        String msgtype = MsgType.NEWS.getMsgtype();
        Map<String, Object> map = new HashMap<>();

        map.put("touser", "oJ1R0w4jz1QaGUMn8SA5n9ud-yIA");
        map.put("msgtype", msgtype);

        Map<String, List<Map<String, String>>> newsMap = new HashMap<>();

        List<Map<String, String>> articles = new ArrayList<>();
        Map<String, String> articlesMap = new HashMap<>();
        articlesMap.put("title", "炭家牛韩式烤肉");
        articlesMap.put("description", "xxx");
        articlesMap.put("url", "https://www.baidu.com/");
        articlesMap.put("picurl", "https://www.meituan.com/meishi/157132690/");

        articles.add(articlesMap);

        newsMap.put("articles", articles);

        map.put("news", newsMap);

        String initParam = JSONObject.toJSONString(map);
        System.out.println(initParam);
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + AccessToken.getToken();
        String result = WXNetUtil.post(url, initParam);
        System.out.println(result);

    }

}
